package com.panopset.db;

import java.io.StringWriter;

/**
 *
 * DataObject associated with a table. Pass the TableDescription to the
 * constructor and call:
 *
 * <pre>
 * super(new SomethingTableDescription&lt;newSomething()&gt;)
 * </pre>
 *
 */
public abstract class DataObject {

    /**
     * Table description.
     */
    private final TableDescription<?> td;

    /**
     * Only call this from an empty constructor.
     * @param tableDescription Table description for this data object.
     */
    public DataObject(final TableDescription<?> tableDescription) {
        td = tableDescription;
    }

    /**
     * @return TableDescription associated with this DataObject.
     */
    public final TableDescription<?> getTableDescription() {
        return td;
    }

    /**
     * Formatted dump of all fields.
     */
    @Override
    public String toString() {
    	StringWriter sw = new StringWriter();
    	sw.append(this.getClass().getCanonicalName());
		sw.append("\n");
    	for (DataField fld : getTableDescription().getFlds()) {
    		sw.append(fld.getName());
    		sw.append(":");
    		sw.append(getTableDescription().getReflection(this, fld).toString());
    		sw.append("\n");
    	}
    	return sw.toString(); 
    }
    /**
     * -1L.
     */
    public static final long NOID = -1L;

    /**
     * id.
     */
    private long id = NOID;

    /**
     * @return id.
     */
    public final long getId() {
        return id;
    }

    /**
     * @param newId
     *            id.
     */
    public final void setId(final long newId) {
        id = newId;
    }
}
